package com.karaoke.android.home.fragment


import android.app.Fragment
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.h6ah4i.android.widget.advrecyclerview.draggable.RecyclerViewDragDropManager
import com.karaoke.android.data.domain.Song
import com.karaoke.android.data.service.MediaListService
import com.karaoke.android.home.BR
import com.karaoke.android.home.R
import com.karaoke.android.home.adapter.MediaEndAdapter
import com.karaoke.android.home.adapter.MediaListAdapter
import com.karaoke.android.home.databinding.FragmentMediaListPageBinding
import com.karaoke.android.home.vm.MediaListPageViewModel
import com.karaoke.android.library.databind.ItemBinding
import com.karaoke.android.library.utils.SpaceItemDecoration
import kotlin.properties.Delegates


/**
 * A simple [Fragment] subclass.
 */
class MediaListPageFragment : Fragment()
{
    var mBinding: FragmentMediaListPageBinding by Delegates.notNull()
    private var mAdapter: RecyclerView.Adapter<*>? = null
    private var mWrappedAdapter: RecyclerView.Adapter<*>? = null
    private var mRecyclerViewDragDropManager: RecyclerViewDragDropManager? = null
    val songBindings: ItemBinding<Song> = ItemBinding.of<Song>(BR.item, R.layout.media_list_item)

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View
    {
        mBinding = FragmentMediaListPageBinding.inflate(inflater, container, false)
        mRecyclerViewDragDropManager = RecyclerViewDragDropManager()
        mRecyclerViewDragDropManager?.setInitiateOnMove(false)
        mRecyclerViewDragDropManager?.setInitiateOnLongPress(true)
        val myItemAdapter: RecyclerView.Adapter<*>
        if (type == 0)
            myItemAdapter = MediaListAdapter(MediaListService.mediaList.list)
        else myItemAdapter = MediaEndAdapter(MediaListService.mediaList.endList)
        mAdapter = myItemAdapter
        mWrappedAdapter = mRecyclerViewDragDropManager?.createWrappedAdapter(myItemAdapter)      // wrap for dragging
        mBinding.listView.layoutManager = GridLayoutManager(activity, 1)
        mBinding.listView.addItemDecoration(SpaceItemDecoration(5))
        mBinding.listView.adapter = mWrappedAdapter  // requires *wrapped* adapter
        mRecyclerViewDragDropManager?.attachRecyclerView(mBinding.listView)
        return mBinding.root
    }

    val viewModel: MediaListPageViewModel by lazy { MediaListPageViewModel(type, activity) }

    //
    companion object
    {
        fun newInstance(type: Int) = MediaListPageFragment().apply {
            this.type = type
        }
    }

    private var type = 0

    override fun onActivityCreated(savedInstanceState: Bundle?)
    {
        super.onActivityCreated(savedInstanceState)
        if (mAdapter?.itemCount == 0)
        {
            if (type == 0)
                mBinding.stateful.showEmpty("来不及解释了，赶紧点歌吧")
            else mBinding.stateful.showEmpty("还没唱呢")
        }
    }

}
